package ar.rhok.yerbus.web.spring;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.support.WebApplicationContextUtils;

import ar.rhok.yerbus.web.MainApp;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;

public class SpringAwareApplicationServlet extends AbstractApplicationServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7423689179082823157L;
	private ApplicationContext applicationContext;

	@Override
	public void init(ServletConfig servletConfig) throws ServletException {
		super.init(servletConfig);
		applicationContext = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());

	}

	@Override
	protected Class<? extends Application> getApplicationClass()
			throws ClassNotFoundException {
		return MainApp.class;
	}

	@Override
	protected Application getNewApplication(HttpServletRequest request)
			throws ServletException {
		return applicationContext.getBean(MainApp.class);
	}

}
